define(["jquery", "template","bbcare","scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"], function ($, template,$bb,$ui,$servers) {
    var event={};
    var clear = function(){
        $("input:checked").each(function(){
            $(this).removeAttr("checked");
        });
    };
    event.jump = function(type){
        $('#myModal').on('hidden.bs.modal', function (e) {
            if(type == "HighriskPregnant"){
                $bb.openPage("highRiskPregnantwomenList");
            }
            if(type == "SDS"){
                $bb.openPage("depressionList");
            }
            if(type == "SAS"){
                $bb.openPage("anxietyList");
            }
            if(type == "EPDS"){
                $bb.openPage("postpartumDepressionList");
            }
        });
    };
    event.countHighRiskScore = function (userId,type){
        var HashMap=$servers.hashmap(),hashMapQues = new HashMap();
        $("#question").find("input:checked").each(function(){
            var score = $(this).attr("score");
            var val = $(this).val();
            var id = $(this).attr("id");
            var name = $(this).attr("name");
            hashMapQues.put(id ,{
                "score":score,
                "id":id,
                "questions":val,
                "name":name
            });
        });
        var postionIds = [],result =[],scoresTotal=0;
        for(var item in hashMapQues.map){
            postionIds.push(hashMapQues.map[item].id);
            scoresTotal += hashMapQues.map[item].score*1;
            result.push({
                "id":hashMapQues.map[item].id,
                "ques":hashMapQues.map[item].questions
            });
        }
        var lens = result.length,level;
        level = scoresTotal < 5 ? "正常": scoresTotal < 10 && scoresTotal >=5 ? "轻度高危":scoresTotal >= 20 ? "重度高危":"中度高危";
        var finRes = new HashMap();
        finRes.put("lens",lens);//项数；
        finRes.put("scoresTotal",scoresTotal);//总分数；
        finRes.put("level",level);//程度；
        finRes.put("result",result);//题目；
        var scoreParam = $servers.scoreParams(type,postionIds,finRes,userId);
        var scoreFn = function (data){
            $('#myModal').modal('show');
            $ui.highRiskResultInit({"id":"#resShow","data":data.data.map});
            clear();
        };
        var toSaveHigh = function(){
            $servers.getScore(scoreParam,scoreFn);
        };
        if(finRes.map.lens == 0){
            $bb.toastConfirmShow(toSaveHigh,'',"该问卷还没有做，您确定要提交么？");
        }else{
            toSaveHigh();
        }
    };
    event.countAdpScore = function(userId,lensTotal,type){
        var result = $servers.scoresDeal("#question",type);
        var scores = result.scores,hashMap = result.hash,lens = result.lens,name=result.name;
        var scoreParam = $servers.scoreParams(type,hashMap,scores,userId);//焦虑
        var scoreFn = function (data){
            $servers.scriptAnswer("#resShow",data.data.map,type);
            clear();
        };
        if(lens != lensTotal){
            if(lens == 0){
                $bb.toastShow("您的问卷还没有做，请全部做完后再提交！",2000);
            }else{
                var not = notDoArr(name ,lensTotal);
                var notDo = not.join("、");
                $bb.toastShow("该问卷第"+notDo +"还没有做，全部做完才可以提交",2000);
            }
        }else{
            $servers.getScore(scoreParam, scoreFn);
        }
    };
    var notDoArr = function(arr,lensTotal){
        var notDo = [];
        for( var i = 1 ; i <= lensTotal ; i++ ){
            if($.inArray(i.toString(),arr) == "-1"){notDo.push(i);}
        }
        return notDo;
    };
    return event;
})